@echo off
setlocal enabledelayedexpansion

echo ==========================================
echo 营养系统菜品信息处理结果检查工具
echo ==========================================

echo.
echo 1. 检查最近的任务执行日志...
echo ================================
if exist "D:\wwwroot\yingyang\runtime\auto_process_dish_info.log" (
    echo 最近10行日志内容:
    powershell "Get-Content 'D:\wwwroot\yingyang\runtime\auto_process_dish_info.log' -Tail 10"
) else (
    echo 日志文件不存在
)

echo.
echo 2. 检查数据库更新结果...
echo ================================
cd /d D:\wwwroot\yingyang
php -r "
<?php
// 数据库连接配置
\$config = include 'application/database.php';
\$dsn = 'mysql:host=' . \$config['hostname'] . ';dbname=' . \$config['database'] . ';charset=' . \$config['charset'];
\$pdo = new PDO(\$dsn, \$config['username'], \$config['password']);
\$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

echo '=== 数据库统计信息 ===' . PHP_EOL;

// 统计总菜品数
\$stmt = \$pdo->query('SELECT COUNT(*) as total FROM fa_food_dish');
\$total = \$stmt->fetch(PDO::FETCH_ASSOC);
echo '总菜品数: ' . \$total['total'] . PHP_EOL;

// 统计已处理菜品数
\$stmt = \$pdo->query('SELECT COUNT(*) as processed FROM fa_food_dish WHERE ai_called = 1');
\$processed = \$stmt->fetch(PDO::FETCH_ASSOC);
echo '已处理菜品数: ' . \$processed['processed'] . PHP_EOL;

// 统计未处理菜品数
\$stmt = \$pdo->query('SELECT COUNT(*) as unprocessed FROM fa_food_dish WHERE ai_called = 0');
\$unprocessed = \$stmt->fetch(PDO::FETCH_ASSOC);
echo '未处理菜品数: ' . \$unprocessed['processed'] . PHP_EOL;

// 显示最近处理的5个菜品
echo PHP_EOL . '=== 最近处理的5个菜品 ===' . PHP_EOL;
\$stmt = \$pdo->query('SELECT id, name, cook_time, steps FROM fa_food_dish WHERE ai_called = 1 ORDER BY updatetime DESC LIMIT 5');
\$dishes = \$stmt->fetchAll(PDO::FETCH_ASSOC);
foreach (\$dishes as \$dish) {
    echo 'ID: ' . \$dish['id'] . ', 名称: ' . \$dish['name'] . PHP_EOL;
    echo '  烹饪时长: ' . \$dish['cook_time'] . PHP_EOL;
    echo '  制作步骤: ' . substr(\$dish['steps'], 0, 50) . '...' . PHP_EOL;
}

// 显示最近的食材关联数据
echo PHP_EOL . '=== 最近的食材关联数据 ===' . PHP_EOL;
\$stmt = \$pdo->query('SELECT r.id, r.dish_id, d.name as dish_name, r.ingredient_name, r.quantity, r.ingredient_id FROM fa_food_ingredientrel r LEFT JOIN fa_food_dish d ON r.dish_id = d.id ORDER BY r.createtime DESC LIMIT 5');
\$relations = \$stmt->fetchAll(PDO::FETCH_ASSOC);
foreach (\$relations as \$rel) {
    echo '关联ID: ' . \$rel['id'] . ', 菜品: ' . \$rel['dish_name'] . ' (' . \$rel['dish_id'] . ')' . PHP_EOL;
    echo '  食材: ' . \$rel['ingredient_name'] . ', 用量: ' . \$rel['quantity'] . ', 食材ID: ' . \$rel['ingredient_id'] . PHP_EOL;
}
?>
"

echo.
echo 3. 检查任务状态...
echo ================================
schtasks /query /tn "营养系统菜品信息自动处理" /fo LIST | findstr "上次运行时间 下次运行时间 状态"

echo.
echo 检查完成。
pause